package com.smarthealthring.mm.widgets.bottombar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.smarthealthring.mm.R;


/**
 * Created by YoKeyword on 16/6/3.
 */
public class BottomBarTab extends FrameLayout {
    private ImageView mIcon;
    private TextView mTvTitle;
    private Context mContext;
    private int mTabPosition = -1;

    private int[] mTabRes = new int[]{R.drawable.healthy0, R.drawable.sport0, R.drawable.fangwei0, R.drawable.yikatong0, R.drawable.mine0};
    private int[] mTabResPressed = new int[]{R.drawable.healthy1, R.drawable.sport1, R.drawable.fangwei1, R.drawable.yikatong1, R.drawable.mine1};

    public BottomBarTab(Context context, @DrawableRes int icon, CharSequence title) {
        this(context, null, icon, title);
    }

    public BottomBarTab(Context context, AttributeSet attrs, int icon, CharSequence title) {
        this(context, attrs, 0, icon, title);
    }

    public BottomBarTab(Context context, AttributeSet attrs, int defStyleAttr, int icon, CharSequence title) {
        super(context, attrs, defStyleAttr);
        init(context, icon, title);
    }

    private void init(Context context, int icon, CharSequence title) {
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.selectableItemBackgroundBorderless});
        Drawable drawable = typedArray.getDrawable(0);
        setBackgroundDrawable(drawable);
        typedArray.recycle();


        View lLContainer = LayoutInflater.from(context).inflate(R.layout.home_tab_content, null);
        mIcon = lLContainer.findViewById(R.id.tab_content_image);
        mTvTitle = lLContainer.findViewById(R.id.tab_content_text);
        mIcon.setImageResource(icon);
        mTvTitle.setText(title);

        addView(lLContainer);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            mIcon.setImageResource(mTabResPressed[mTabPosition]);
            mTvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.tabtext_select));
        } else {
            mIcon.setImageResource(mTabRes[mTabPosition]);
            mTvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.tabtext_unselect));
        }
    }

    public void setTabPosition(int position) {
        mTabPosition = position;
        if (position == 0) {
            setSelected(true);
        }
    }

    public int getTabPosition() {
        return mTabPosition;
    }
}
